package cn.ulyer.common.transcode.secret;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import cn.hutool.crypto.symmetric.AES;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * AES处理
 */
@Component
public class AESHandler implements TranscodeHandler{

    @Override
    public String encryptHex(Object key, String sourceText) {
        AES aes = new AES(SecureUtil.decode((String) key));
        return aes.encryptHex(sourceText.getBytes());
    }

    @Override
    public String decrypt(Object key, String hexOrBase64) {
        AES aes = new AES(((String) key).getBytes());
        return aes.decryptStr(hexOrBase64);
    }

    @Override
    public String algorithm() {
        return "AES";
    }

    @Override
    public byte[] signature(Object key, String source) {
        AES aes = SecureUtil.aes(SecureUtil.decode((String) key));
        return aes.encrypt(source);
    }

    @Override
    public boolean verifySign(Object key, String source, String signData) {
        byte[] sign = SecureUtil.decode(signData);
        byte[] s = source.getBytes(StandardCharsets.UTF_8);
        return StringUtils.equals(new String(sign),new String(s));
    }


}
